<?
require_once('./application/classes/item.class.php');
require_once('./application/classes/item/activiteiten.class.php');
require_once('./application/classes/item/bericht.class.php');
require_once('./application/classes/item/video.class.php');
require_once('./application/classes/item/foto.class.php');
require_once('./application/classes/item/album.class.php');
require_once('./application/classes/item/zoeker.class.php');

class zoekenModel extends db implements db_interface{
	private static $instance;
	private $term;

	public function __construct($iDb){
	
			parent::__construct($iDb);
	
	}
	
	public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	public function getBerichten($iSearch){
		$query = $this -> db -> prepare('select count(bericht_text) from item join bericht on (item_id = bericht_id)  where bericht_text like ? or item_title like ? AND item_deleted = 1;');
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id); 
		
		
		$query -> fetch();
		
		return $id;
	}
	public function getNieuws($iSearch){
		$query = $this -> db -> prepare('select count(nieuws_id) from item join nieuws on (item_id = nieuws_id)  where nieuws_bericht like ? or item_title like ? AND item_deleted = 1;');
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id); 
		
		
		$query -> fetch();
		
		return $id;
	}
	public function getActiviteiten($iSearch){
		$query = $this -> db -> prepare('select count(activi_id) from item join activiteiten on (item_id = activi_id)  where activi_beschrijving like ? or item_title like ? AND item_deleted = 1;');
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id); 
		
		
		$query -> fetch();
		
		return $id;
	}
	public function getZoekertjes($iSearch){
		$query = $this -> db -> prepare('select count(zoeker_id) from item join zoeker on (item_id = zoeker_id)  where zoeker_beschrijving like ? or item_title like ? AND item_deleted = 1;');
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id); 
		
		
		$query -> fetch();
		
		return $id;
	}
	public function getItems($iSearch){
		$query = $this -> db -> prepare('select  item_type,count(item_id) from item where item_title like ? AND item_deleted = 1 group by item_type;');
		$bindValue = '%'.$iSearch.'%';
		$query -> bind_param('s', $bindValue);
		$query -> execute();
		
		$query -> bind_result($item_type,$id); 
		
		$returnArray = array();
		while($query -> fetch())
		{	
			$returnArray[] = array('type' => $item_type, 'id' => $id);
		}
		return $returnArray;	
	}
	
	
	
	public function getSearchedBerichten($iSearch){
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join bewonersgroep bg on (i.item_geb_id = bg.bew_id) join bericht b on (i.item_id = b.bericht_id) where bericht_text like ? or item_title like ? AND i.item_deleted = 1;');
		
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$text,$date); 
		
		$returnArray = array();
		while ($query -> fetch())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $id;
			$bericht -> title = $title;
			$bericht -> geb_naam = $geb_naam;
			$bericht -> bericht_text = $text;
			$bericht -> geb_id = $id;
			$bericht -> datum = $date;
			$returnArray[] = $bericht;
		}
		
		return $returnArray;
		
	}
	public function getSearchedFotos($iSearch){
	$query = $this -> db -> prepare('SELECT i.item_id,b.bew_titel,b.bew_id,i.item_title,f.foto_path,f.foto_album_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y"),(SELECT item_title FROM item WHERE item_id = f.foto_album_id) as album_naam FROM item i join bewonersgroep b on (i.item_geb_id = b.bew_id) join foto f on (i.item_id = f.foto_id)  WHERE i.item_title like ? AND i.item_deleted = 1;');
		
		$bindValue = '%'.$iSearch.'%';
		$query -> bind_param('s', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$geb_id,$title,$path,$album_id,$date,$album_name); 
		$array = array();
		while ($query -> fetch())
		{
			$fotos = new item_foto($this -> db);
			$fotos -> id = $id;
			$fotos -> foto_album_naam = $album_name;
			$fotos -> foto_album_id = $album_id;
			$fotos -> title = $title;
			$fotos -> geb_naam = $geb_naam;
			$fotos -> foto_path = $path;
			$fotos -> geb_id = $geb_id;
			$fotos -> datum = $date;
			
			$array[] = $fotos;
		}
		
return $array;
		
		
	}
	public function getSearchedAlbums($iSearch){
		$album = array();
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,bg.bew_id,i.item_title,a.album_type,DATE_FORMAT(i.item_datum, "%H:%i-%d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join album a on (i.item_id = a.album_id) join bewonersgroep bg on (g.geb_id = bg.bew_id) WHERE i.item_title like ?  AND i.item_deleted = 1;');
		
		$bindValue = '%'.$iSearch.'%';
		$query -> bind_param('s', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$bew_titel,$bew_id,$title,$type,$datum); 
		
		while ($query -> fetch())
		{
			$albums = new item_album($this -> db);
			$albums -> id = $id;
			$albums -> album_type= $type;
			$albums -> title = $title;
			$albums -> geb_naam = $bew_titel;
			$albums -> geb_id = $bew_id;
			$albums -> datum = $datum;
			$album[] = $albums;
		}
		
	return $album;
	}
	public function getSearchedNieuws($iSearch){
		$query = $this -> db -> prepare('SELECT i.item_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,i.item_title,n.nieuws_bericht,g.geb_naam, g.geb_id from item i join nieuws n on(i.item_id = n.nieuws_id)join gebruikers g on(i.item_geb_id = g.geb_id) where n.nieuws_bericht like ? or i.item_title like ? AND i.item_deleted = 1;');
		
	
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id, $date, $title, $nieuws_bericht, $geb_naam, $geb_id);
		
		$returnArray = array();
		
		
		while ($query -> fetch())
		{
			$nieuws = new item_nieuws($this -> db);
			$nieuws -> id = $id;
			$nieuws -> title = $title;
			$nieuws -> geb_naam = $geb_naam;
			$nieuws -> nieuws_text = $nieuws_bericht;
			$nieuws -> geb_id = $geb_id;
			$nieuws -> datum = $date;
			$returnArray[]= $nieuws;
		}
		return $returnArray;
	}
	public function getSearchedVideos($iSearch){
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,bg.bew_id,i.item_title,v.video_youtube_id, v.video_album_id, DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,(SELECT item_title FROM item WHERE item_id = v.video_album_id) as album_naam FROM item i  join bewonersgroep bg on (bg.bew_id = i.item_geb_id) join video v on (i.item_id = v.video_id) WHERE i.item_title like ? AND i.item_deleted = 1;');
		
		$videos = array();
		
		$bindValue = '%'.$iSearch.'%';
		$query -> bind_param('s', $bindValue);
		$query -> execute();
		
		$query -> bind_result($item_id,$geb_naam,$geb_id,$item_title,$youtube_id,$album,$date,$album_naam); 
		
		while ($query -> fetch())
		{
			$video = new item_video($this -> db);
			$video -> id = $item_id;
			$video -> title = $item_title;
			$video -> geb_naam = $geb_naam;
			$video -> youtube_id = $youtube_id;
			$video -> geb_id = $geb_id;
			$video -> datum = $date;
			$video -> album_id = $album;
			$video -> album_naam = $album_naam;
			$videos[] = $video;
		}
		
		return $videos;
	}
	public function getSearchedActivi($iSearch){
		$query = $this -> db -> prepare('select i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as "date",i.item_geb_id,DATE_FORMAT(a.activi_datum,"%d/%m/%Y") as aDate,a.activi_beschrijving,b.bew_titel from item i join activiteiten a on (i.item_id = a.activi_id) join bewonersgroep b on(i.item_geb_id = b.bew_id)  where a.activi_beschrijving like ? or item_title like ? AND i.item_deleted = 1;');
		
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id,$title,$date,$geb_id,$aDate,$text,$naam); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$activi = new item_activiteiten($this -> db);
			$activi -> id = $id;
			$activi -> title = $title;
			$activi -> geb_naam = $naam;
			$activi -> act_beschrijving = $text;
			$activi -> geb_id = $geb_id;
			$activi -> datum = $date;
			$activi -> act_date = $aDate;
			$returnArray[] = $activi;
		}
		
		return $returnArray;
	}
	public function getSearchedZoekertjes($iSearch){
		$query = $this -> db -> prepare('SELECT i.item_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date,i.item_title,z.zoeker_beschrijving,z.zoeker_type,b.bew_id,b.bew_titel from item i join zoeker z on(i.item_id = z.zoeker_id)join bewonersgroep b on(i.item_geb_id = b.bew_id) where zoeker_beschrijving like ? or item_title like ? AND item_deleted = 1;');
		
		$bindingvars=array('%'.$iSearch.'%','%'.$iSearch.'%');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
		$query -> bind_result($id,$date,$title,$zoeker,$zoeker_type,$bew_id,$naam); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$zoeker = new item_zoeker($this -> db);
			$zoeker -> id = $id;
			$zoeker -> title = $title;
			$zoeker -> geb_naam = $naam;
			$zoeker -> zoeker_text = $zoeker;
			$zoeker -> geb_id = $bew_id;
			$zoeker -> datum = $date;
			$zoeker -> zoeker_type = $zoeker_type;
			$returnArray[] = $zoeker;
			
		}
		
		return $returnArray;
}
}


?>